package com.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.auth.mapper.SysRoleMapper;
import com.auth.model.system.SysRole;
import com.auth.model.system.SysUserRole;
import com.auth.service.SysRoleService;
import com.auth.service.SysUserRoleService;
import com.auth.vo.system.AssginRoleVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author:Tangjiachang
 * @Date 2023/7/12
 * @Description:
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper,SysRole> implements SysRoleService {
    public final SysRoleMapper sysRoleMapper;
    public final SysUserRoleService sysUserRoleService;
    public SysRoleServiceImpl(SysRoleMapper sysRoleMapper,
                              SysUserRoleService sysUserRoleService) {
        this.sysRoleMapper = sysRoleMapper;
        this.sysUserRoleService = sysUserRoleService;
    }
    @Override
    public Map<String, Object> findRoleByUserId(Long userId) {
        List<SysRole> allRole = baseMapper.selectList(null);
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, userId);
        List<SysUserRole> userRoles = sysUserRoleService.list(queryWrapper);

        List<Long> roleIds = userRoles.stream()
                .map(SysUserRole::getRoleId)
                .collect(Collectors.toList());

        List<SysRole> sysRoles = Lists.newArrayList();
        allRole.forEach(item -> {
            if (roleIds.contains(item.getId())) {
                sysRoles.add(item);
            }
        });

        return new HashMap<String, Object>(0) {{
            put("assignRoleList", sysRoles);
            put("allRoleList", allRole);
        }};
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object doAsking(AssginRoleVo assginRoleVo) {
        try {
            LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysUserRole::getUserId, assginRoleVo.getUserId());
            sysUserRoleService.remove(queryWrapper);
            List<Long> allRole = assginRoleVo.getRoleIdList();
            List<SysUserRole> saveSysUserRole = Lists.newArrayList();
            for (Long roleId : allRole) {
                if (null == roleId) {
                    continue;
                }
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setRoleId(roleId);
                sysUserRole.setUserId(assginRoleVo.getUserId());
                saveSysUserRole.add(sysUserRole);
            }
            sysUserRoleService.saveBatch(saveSysUserRole);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            //手工回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return null;
        }

    }
}
